﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.ComponentModel;
using System.Reflection;
using System.Web;
using System.Web.Compilation;
using System.IO;
using System.Collections.Specialized;
using System.Linq.Expressions;
using System.Data;

namespace System.Web.Mvc {
    public static class RadioListExtension {
        
        #region Radio Button List
        /// <summary>
        /// Creates an HTML Radio button list based on the passed-in datasource.
        /// </summary>
        /// <param name="dataSource">IEnumerable, IQueryable, DataSet, DataTable, or IDataReader</param>
        /// <param name="htmlName">The name of the control for the page</param>
        public static string[] RadioButtonList(this HtmlHelper helper, string htmlName, object dataSource) {
            return RadioButtonList(helper,htmlName,dataSource,"","",null,null);
        }
        /// <summary>
        /// Creates an HTML Radio button list based on the passed-in datasource.
        /// </summary>
        /// <param name="dataSource">IEnumerable, IQueryable, DataSet, DataTable, or IDataReader</param>
        /// <param name="htmlName">The name of the control for the page</param>
        /// <param name="textField">The datasource field to use for the the display text</param>
        /// <param name="valueField">The datasource field to use for the the control value</param>
        /// <param name="selectedValue">The value that should be selected</param>
        /// <param name="htmlAttributes">Any attributes you want set on the tag. Use anonymous-type declaration for this: new{class=cssclass}</param>
        public static string[] RadioButtonList(this HtmlHelper helper, string htmlName, object dataSource, object selectedValue) {

            return RadioButtonList(helper, htmlName, dataSource, "", "", selectedValue, null);
        }

        public static string[] RadioButtonList(this HtmlHelper helper, string htmlName, object dataSource, string textField, string valueField) {
            return RadioButtonList(helper, htmlName, dataSource, textField, valueField, null, null);
        }
        public static string[] RadioButtonList(this HtmlHelper helper, string htmlName, object dataSource, string textField, string valueField, object selectedValue) {
            return RadioButtonList(helper, htmlName, dataSource, textField, valueField, selectedValue, null);
        }

        /// <summary>
        /// Creates an HTML Radio button list based on the passed-in datasource.
        /// </summary>
        /// <param name="dataSource">IEnumerable, IQueryable, DataSet, DataTable, or IDataReader</param>
        /// <param name="htmlName">The name of the control for the page</param>
        /// <param name="textField">The datasource field to use for the the display text</param>
        /// <param name="valueField">The datasource field to use for the the control value</param>
        /// <param name="selectedValue">The value that should be selected</param>
        /// <param name="htmlAttributes">Any attributes you want set on the tag. Use anonymous-type declaration for this: new{class=cssclass}</param>
        public static string[] RadioButtonList(this HtmlHelper helper, string htmlName, object dataSource, string textField, string valueField, object selectedValue, object htmlAttributes) {
            //input formats

            Hashtable setHash = HtmlExtensionUtility.GetPropertyHash(htmlAttributes);

            //output
            StringBuilder sb = new StringBuilder();
            DataTable tbl = MvcControlDataBinder.EvalDataSource(dataSource, textField, valueField);

            string attributeList = string.Empty;
            if (setHash != null)
                attributeList = setHash.ToAttributeList();

            //loop the source
            foreach (DataRow dr in tbl.Rows) {
                string selectedFlag = string.Empty;
                string thisValue = dr[0].ToString();
                string thisText = dr[1].ToString();
                bool isChecked = false;
                
                isChecked = HtmlExtensionUtility.AreEqual(selectedValue, thisValue);

                //append a pipe for Split() later
                string radioButton = RadioButton(helper, htmlName, thisText, thisValue, isChecked, htmlAttributes) + "|";
                sb.Append(radioButton);
            }
            string result = sb.ToString();
            string[] resultList = result.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
            return resultList;
        }

        #endregion
        
        #region Radio button
        /// <summary>
        /// Creates a radio button input for your form
        /// </summary>
        /// <param name="htmlName">The name and ID of the control</param>
        /// <param name="value">The text for the control</param>
        /// <param name="isChecked">Whether the radio button is selected</param>
        public static string RadioButton(this HtmlHelper helper, string htmlName, string text, string value, bool isChecked) {
            return RadioButton(helper, htmlName, text, value, isChecked, null);

        }
        /// <summary>
        /// Creates a radio button input for your form
        /// </summary>
        /// <param name="htmlName">The name and ID of the control</param>
        /// <param name="test">The text & value for the control</param>
        public static string RadioButton(this HtmlHelper helper, string htmlName, string text) {
            return RadioButton(helper, htmlName, text, text, false, null);
        }
        /// <summary>
        /// Creates a radio button input for your form
        /// </summary>
        /// <param name="htmlName">The name and ID of the control</param>
        /// <param name="test">The text & value for the control</param>
        /// <param name="value">The text for the control</param>
        public static string RadioButton(this HtmlHelper helper, string htmlName, string text, string value) {
            return RadioButton(helper, htmlName, text, value, false, null);
        }

        /// <summary>
        /// Creates a radio button input for your form
        /// </summary>
        /// <param name="htmlName">The name and ID of the control</param>
        /// <param name="value">The text for the control</param>
        /// <param name="isChecked">Whether the radio button is selected</param>
        public static string RadioButton(this HtmlHelper helper, string htmlName, string text, bool isChecked) {
            return RadioButton(helper, htmlName, text, text, isChecked, null);
        }
        /// <summary>
        /// Creates a radio button input for your form
        /// </summary>
        /// <param name="htmlName">The name and ID of the control</param>
        /// <param name="text">The text for the control</param>
        /// <param name="value">The value for the control</param>
        /// <param name="isChecked">Whether the radio button is selected</param>
        /// <param name="htmlAttributes">Any attributes you want set on the tag. Use anonymous-type declaration for this: new{class=cssclass}</param>
        public static string RadioButton(this HtmlHelper helper, string htmlName, string text, string value, bool isChecked, object htmlAttributes) {
            string radioFormat = "<input type=\"radio\" id=\"{0}\" name=\"{0}\" value=\"{1}\" {2} {3}/>{4}";

            string atts = string.Empty;
            if (htmlAttributes != null)
                atts = htmlAttributes.ToAttributeList();

            string checkedFlag = isChecked ? "checked=checked" : "";

            return string.Format(radioFormat, htmlName, helper.Encode(value), checkedFlag, atts, helper.Encode(text));

        }
        #endregion

    }
}
